package satir.card.factory;

import java.util.ArrayList;
import java.util.Hashtable;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import satir.card.Choice;
import satir.card.Question;
import satir.framework.refectoring.Factory;

/**
 * A factory for creating Question objects.
 * @version 3.0 April 2012
 * @author Creative Family
 */
public class QuestionFactory implements Factory {

    /** The card json. */
    protected JSONObject json;

    /**
     * Creates the.
     * @param json
     *            the json
     * @return the object
     * @see Factory, LazyInitiator
     */
    @Override
    public Object create(final JSONObject json) {
	JSONArray choiceJsonArr;
	try {
	    choiceJsonArr = json.getJSONArray("choice");
	    ArrayList<Choice> choices = new ArrayList<Choice>();
	    for (int j = 0; j < choiceJsonArr.length(); j++) {
		JSONObject choiceJson = choiceJsonArr.getJSONObject(j);
		Hashtable<String, Integer> factorTmp = new Hashtable<String, Integer>();
		factorTmp.put(choiceJson.getString("factor"),
			choiceJson.getInt("score"));
		choices.add(new Choice(choiceJson.getString("content"),
			choiceJson.getInt("rewardSE"), factorTmp));
	    }
	    String contentOfQuestion = json.getString("question");
	    return new Question(contentOfQuestion, choices);
	} catch (JSONException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}
	return null;
    }
}
